package com.example.demo.hcontroller;

import com.example.demo.pojo.Chose;
import com.example.demo.pojo.Topic;
import com.example.demo.service.IChoseService;
import com.example.demo.service.ITopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping(value = "/hChose")
public class HChoseController {

    @Autowired
    private IChoseService ChoseService;

    @Autowired
    private ITopicService TopicService;
    @RequestMapping("/toChose")
    private String toChose(Model model, Chose Chose) {
        List<Chose> list = ChoseService.selectChose();
        model.addAttribute("choselist", list);
        List<Topic> tlist=TopicService.selectTopic();
        model.addAttribute("topiclist", tlist);
        return "chose";
    }

    @RequestMapping("/toaddChose")
    private String toaddChose(Model model, Chose Chose) {
        List<Topic> tlist=TopicService.selectTopic();
        model.addAttribute("topiclist", tlist);
        return "addchose";
    }

    @RequestMapping("/addChose")
    private String addChose(Model model, Chose Chose, String answer, Integer num, Integer tid) {
        Chose.setAnswer(answer);
        Chose.setNum(num);
        Chose.setTid(tid);
        ChoseService.insertSelective(Chose);
        return "redirect:toChose";
    }

    @RequestMapping("/toupdateChose")
    private String toupdateChose(Model model, Chose Chose,Integer id) {
        model.addAttribute("chose", ChoseService.selectByPrimaryKey(id));
        return "updatechose";
    }

    @RequestMapping("/updateChose")
    private String updateChose(Model model, Chose Chose, String answer, Integer num, Integer id) {
        Chose.setAnswer(answer);
        Chose.setNum(num);
        Chose.setId(id);
        ChoseService.updateByPrimaryKeySelective(Chose);
        return "redirect:toChose";
    }

    @RequestMapping("/deleteChose")
    private String deleteChose(Model model, Chose Chose, Integer id) {
        ChoseService.deleteByPrimaryKey(id);
        return "redirect:toChose";
    }
}
